package com.example.dbpage

import android.arch.lifecycle.Observer
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.DividerItemDecoration
import android.util.Log
import android.view.View
import com.example.dbpage.adapter.RecyclerAdapter
import com.example.dbpage.adapter.RecyclerPageAdapter
import com.example.dbpage.bean.Users
import com.example.dbpage.tool.DaoHelp
import kotlinx.android.synthetic.main.activity_list.*
import java.util.*

class ListActivity : AppCompatActivity() {

    private lateinit var dao: DaoHelp

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_list)

        val adapter = RecyclerAdapter()


        recyclerView.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))
        /*recyclerView.adapter = adapter
        adapter.submitList(getData())*/

        val adapter2 = RecyclerPageAdapter()

        recyclerView.adapter = adapter2

        val model = RViewModel(application)

        model.getData().observe(this, Observer {
            adapter2.submitList(it)
        })


        dao = DaoHelp.get(this)

        /*dao.userDao().getAllUsers3().observe(this, Observer {
            it?.forEach {
                say(it.name)
            }
        })


        dao.deviceDao().getAllDevice().observe(this, Observer {
            //it?.forEach { say(it.carname) }
            it?.forEach { say(it.carname + "----" + it.cartype + "----" + it.carbrand) }
        })*/

    }


    fun insert(view: View) {

        val rand = Random()

        val age = rand.nextInt(100)

        dao.userDao().insert(Users(age, "随机$age"))
    }


    fun getData(): List<String> {

        val list = ArrayList<String>()

        for (i in 'a'..'z') {
            list.add(i.toString())
        }

        return list
    }

    companion object {
        fun say(body: String) {
            Log.i("xxx", body)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
    }
}
